define([
	"jquery",
	"bootstrap",
	"template",
	"tpl/application.tpl",
	"tpl/navbar.tpl",
	"model/account",
	"model/navbar"],
function($,$bootstrap,$template,tpl_app,tpl_nav,Account){
	var defaults,home = {};
	
	/**
	 * 获取当前登录的账号信息
	 */
	var getCurrUser = function(){
		var account = sessionStorage.account;
		if(account==null){
			window.location.href = "login.html?code=301";
		}else{
			account = eval("("+account+")");
			account = new Account(account);
		}
		return account;
	}
	
	/**
	 * 初始化全局UI
	 */
	var initUI = function(){
		//系统租户名称
		$("#tenant_name").text(defaults.account.tenantName);
		//初始化最小化导航栏事件
		$(".sidebar-toggle").click(onToggleNavbar);
		//初始化登出事件
		$("#logout").click(onLogout);
		//初始化当前账号菜单栏
		$('.dropdown-toggle').dropdown();
		//初始化应用列表
		tpl_app.init('bb-tenant');
		$('#bb-tenant').html($template("tpl_application_dropdown",{apps:defaults.account.apps}));
		//初始化应用菜单
		tpl_nav.init('left-navbar');
		$('#left-navbar').html($template("tpl_navbar",{navs:defaults.account.navs}));
	}
	
	/**
	 * 刷新导航栏
	 */
	var refreshNavbar = function(){
		$navbar.init("left-navbar",{
			url:navUrl,
			initSuccess:function(){
				$("#load-layer").hide();
				$("header").show();
				$("#container").show();
			},
			initFail:function(){
				window.location.href = "login.html";
			}
		});
	}
	
	/**
	 * 最小化(恢复)导航栏
	 */
	var onToggleNavbar = function(){
		$("#bb-tenant").toggleClass("mini");
		$(".sidebar-lc").toggleClass("mini");
		$("#content").toggleClass("max");
	}
	
	/**
	 * 退出系统处理
	 */
	var onLogout = function(){
		window.location.href = "login.html";
	}
	
	/**
	 * 初始化方案
	 */
	home.init = function(){
		defaults = {
			account:getCurrUser()
		};
		initUI();
	}
	
	return home;
});